package com.example.demo.entity.security;

import com.alibaba.fastjson.JSON;
import com.example.demo.entity.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @description: 登录失败的返回
 * @author: cj
 * @create: 09-02-08 09:31:05
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException {
        Result result = new Result();
        result.setStatus( 500 );
        result.setMessage( "登录失败:" + e.getMessage() );
        response.setCharacterEncoding( "UTF-8" );
//        response.setCharacterEncoding( "GBK" );
        response.getWriter().write( JSON.toJSONString( result ) );
    }
}
